package com.duoduo.demo.springnoxml;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Rabbitmq配置
 * <p>
 * https://www.jianshu.com/p/e1258c004314
 * </p>
 * @author chengesheng@kedacom.com
 * @date 2018年3月14日下午4:00:03
 */
@Configuration
public class RabbitmqConfig {

	@Value("${app.rabbitmq.exchange}")
	private String exchangeName;
	@Value("${app.rabbitmq.routingkey1}")
	private String routingKey1;
	@Value("${app.rabbitmq.routingkey2}")
	private String routingKey2;
	@Value("${app.rabbitmq.queue1}")
	private String queueName1;
	@Value("${app.rabbitmq.queue2}")
	private String queueName2;

	// 声明队列
	@Bean
	public Queue queue1() {
		return new Queue(queueName1, true); // true表示持久化该队列
	}

	@Bean
	public Queue queue2() {
		return new Queue(queueName2, true);
	}

	//
	/**
	 * 声明交互器
	 * 
	 * <pre>
	 * 常用交换器类型如下：
	 * Direct(DirectExchange)：direct 类型的行为是"先匹配, 再投送". 即在绑定时设定一个 routing_key, 消息的routing_key完全匹配时, 才会被交换器投送到绑定的队列中去。
	 * Topic(TopicExchange)：按规则转发消息（最灵活）。
	 * Headers(HeadersExchange)：设置header attribute参数类型的交换机。
	 * Fanout(FanoutExchange)：转发消息到所有绑定队列。
	 * </pre>
	 * 
	 * @return
	 */
	@Bean
	public TopicExchange topicExchange() {
		return new TopicExchange(exchangeName);
	}

	// 绑定
	@Bean
	public Binding binding1() {
		return BindingBuilder.bind(queue1()).to(topicExchange()).with(routingKey1);
	}

	@Bean
	public Binding binding2() {
		return BindingBuilder.bind(queue2()).to(topicExchange()).with(routingKey2);
	}

}
